package com.kdd.campus.sh.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @Auther: fcp
 * @Date: 2020/5/23 08:51
 * @Description:
 */
@Configuration
public class RabbitmqConfig {
    @Autowired
    Environment environment;

    @Bean
    public Exchange orderExchange(){
        return new DirectExchange(environment.getProperty("mq.pay.order.exchange"), true, false);
    }

    @Bean
    public Queue orderQueue(){
        return new Queue(environment.getProperty("mq.pay.order.queue"), true);
    }

    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(environment.getProperty("mq.pay.order.routingKey")).noargs();
    }

    @Bean
    public Queue delayQueue(){
        return QueueBuilder
                .durable("delayQueue")
                .withArgument("x-dead-letter-exchange","orderDeadLetterExchange")
                .withArgument("x-dead-letter-routing-key","overOrderQueue")
                .build();
    }

    @Bean
    public Exchange orderDeadLetterExchange(){
        return new DirectExchange("orderDeadLetterExchange",true,false);
    }

    // 存放超时订单的队列
    @Bean
    public Queue overOrderQueue(){
        return new Queue("overOrderQueue", true);
    }

    @Bean
    public Binding overOrderBinding(Queue overOrderQueue,Exchange orderDeadLetterExchange){
        return BindingBuilder.bind(overOrderQueue).to(orderDeadLetterExchange).with("overOrderQueue").noargs();
    }

    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
